/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.Album;
import POJOS.Artist;
import POJOS.MusicGroup;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

/**
 *
 * @author jim
 */
public class ListAlbumArtistForm extends javax.swing.JFrame {
     public static int selectedRow1;
     Album album;
     Artist artist;

    /**
     * Creates new form ListAlbumArtistForm
     */
    public ListAlbumArtistForm() {
        initComponents();
        checkButtons();
        //Δικαίωμα επιλογής μίας γραμμής στον πίνακα
        albumArtistTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    
    
    

    private void checkButtons(){
        selectedRow1= albumArtistTable.getSelectedRow();
        modifyArtist.setEnabled(selectedRow1>=0);
        
        if (selectedRow1>=0){
            album= albumList.get(albumArtistTable.convertRowIndexToModel(selectedRow1));
        }
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        albumQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager.createQuery("SELECT al FROM Album al WHERE al.idMusicGroup IS NULL");
        albumList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(albumQuery.getResultList());
        jScrollPane1 = new javax.swing.JScrollPane();
        albumArtistTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        addOrRemov = new javax.swing.JButton();
        modifyArtist = new javax.swing.JButton();
        exit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ΑΡΧΕΙΟ ΑΛΜΠΟΥΜ ΚΑΛΛΙΤΕΧΝΩΝ");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, albumList, albumArtistTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("ΤΙΤΛΟΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${typeAlbum}"));
        columnBinding.setColumnName("ΤΥΠΟΣ ΑΛΜΠΟΥΜ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idMusicProductionCompany.nameMpc}"));
        columnBinding.setColumnName("ΕΤΑΙΡΙΑ ΠΑΡΑΓΩΓΗΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idArtist.artisticName}"));
        columnBinding.setColumnName("ΚΑΛΛΙΤΕΧΝΗΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${releaseDate}"));
        columnBinding.setColumnName("ΗΜΕΡΟΜΗΝΙΑ ΚΥΚΛΟΦΟΡΙΑΣ");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        albumArtistTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                albumArtistTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(albumArtistTable);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 0, 204));
        jLabel1.setText("ΠΙΝΑΚΑΣ ΑΛΜΠΟYΜ ΚΑΛΛΙΤΕΧΝΩΝ");

        addOrRemov.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        addOrRemov.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add-remove-user-icons-20638746.jpg"))); // NOI18N
        addOrRemov.setText("ΠΡΟΣΘΗΚΗ- ΔΙΑΡΑΦΗ");
        addOrRemov.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addOrRemovActionPerformed(evt);
            }
        });

        modifyArtist.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        modifyArtist.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/hp-customization-control-panel-icon.png"))); // NOI18N
        modifyArtist.setText("ΤΡΟΠΟΠΟΙΗΣΗ");
        modifyArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyArtistActionPerformed(evt);
            }
        });

        exit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Button-exit-icon.jpg"))); // NOI18N
        exit.setText("ΕΞΟΔΟΣ");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addOrRemov)
                        .addGap(88, 88, 88)
                        .addComponent(modifyArtist)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                        .addComponent(exit)))
                .addGap(19, 19, 19))
            .addGroup(layout.createSequentialGroup()
                .addGap(226, 226, 226)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addOrRemov, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modifyArtist)
                    .addComponent(exit))
                .addGap(31, 31, 31))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void albumArtistTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_albumArtistTableMouseClicked
        // TODO add your handling code here:
        checkButtons();        
    }//GEN-LAST:event_albumArtistTableMouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        //Με κλικ στο GUI εκτός πίνακα απενεργοποιείται το κουμπί για τροποποίηση άλμπουμ και ενεργοποιείται η προσθήκη νέου άλμπουμ
        selectedRow1=-1;
        modifyArtist.setEnabled(selectedRow1>=0);
    }//GEN-LAST:event_formMouseClicked

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        // TODO add your handling code here:
        //Επιβεβαίωση για έξοδο από το GUI
        int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε έξοδο από τον πίνακα άλμπουμ καλλιτεχνών;","",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); 
        
        if (choice==0){
            
            dispose();
        }else
            checkButtons();
    }//GEN-LAST:event_exitActionPerformed

    private void modifyArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyArtistActionPerformed
        // TODO add your handling code here:
        selectedRow1= albumArtistTable.getSelectedRow();
        album= albumList.get(albumArtistTable.convertRowIndexToModel(selectedRow1));
        new NewOrModifyAlbumArtistForm(album).setVisible(true);
    }//GEN-LAST:event_modifyArtistActionPerformed

    private void addOrRemovActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addOrRemovActionPerformed
        // TODO add your handling code here:
        //Αν δεν υπάρχει επιλεγμένη γραμμή στον πίνακα των άλμπουμ καλεί το GUI για τη δημιουργία νέου άλμπουμ
        if (selectedRow1<0 ){
            new NewOrModifyAlbumArtistForm().setVisible(true);
        }else{
        //Αν υπάρχει επιλεγμένη γραμμή στον πίνακα διαγράφει το επιλεγμένο άλμπουμ
            selectedRow1= albumArtistTable.getSelectedRow();
            album= albumList.get(albumArtistTable.convertRowIndexToModel(selectedRow1));
        
            int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε να διαγράψετε το album " + album.getTitle() + ""
                +  " ;","",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        
            if (choice==0){
                //Αφαίρεση άλμπουμ από τον καλλιτέχνη
                if(DB.DBmanager.DeleteArtistAlbum(album, album.getIdArtist())){
                 //Αν διαγραφεί το άλμπουμ από τη βάση, εμφανίζει επιτυχή διαγραφή του άλμπουμ
                    if (DB.DBmanager.DeleteAlbum(album)){
                    
                        albumList.remove(album);
                        JOptionPane.showMessageDialog(null,"Επιτυχής διαγραφή album!","SUCCESSFULL",JOptionPane.INFORMATION_MESSAGE);
                        }
                }else{
                    JOptionPane.showMessageDialog(null,"Αποτυχία διαγραφής album!","ERROR",JOptionPane.ERROR_MESSAGE);
                }           
        checkButtons();    
            }                                               
        }
    }//GEN-LAST:event_addOrRemovActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListAlbumArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListAlbumArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListAlbumArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListAlbumArtistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListAlbumArtistForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationPUEntityManager;
    private javax.swing.JButton addOrRemov;
    public static javax.swing.JTable albumArtistTable;
    public static java.util.List<POJOS.Album> albumList;
    private javax.persistence.Query albumQuery;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton modifyArtist;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
